package com.qfedu.wfx.manager.user.service.impl;

import com.qfedu.wfx.manager.user.configuration.LoginUserBean;
import com.qfedu.wfx.manager.user.mapper.UserMapper;
import com.qfedu.wfx.manager.user.po.SysModule;
import com.qfedu.wfx.manager.user.service.IUserService;
import com.qfedu.wfx.manager.vo.ParentNodeVO;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService{
    @Resource
    private UserMapper userMapper;
    @Override
    public LoginUserBean login(String username, String password) throws NullPointerException, AuthenticationException {
        if (username == null){
            throw new NullPointerException();
        }
        if (password == null){
            throw new NullPointerException();
        }
        LoginUserBean loginUserBean = userMapper.checkUserName(username);
        if (loginUserBean == null){
            throw new UnknownAccountException();
        }
        String salt = loginUserBean.getAccount()+loginUserBean.getUserId();
        Md5Hash hash = new Md5Hash(password, salt);
        String hex = hash.toHex();
        if (!hex.equals(loginUserBean.getPassword())){
            throw new IncorrectCredentialsException();
        }
        return loginUserBean;
    }

    /**
     * 查询用户未拥有的权限
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public List<SysModule> queryAllNoHasModule(String roleId){
        List<SysModule> allModule = userMapper.queryAllModule();
        List<String> list = userMapper.queryModuleCodeByRole(roleId);
        ArrayList<SysModule> arrayList = new ArrayList<>();
        for (int i = 0;i<allModule.size();i++){
            if (!list.contains(allModule.get(i).getModuleCode())){
                arrayList.add(allModule.get(i));
            }
        }
        return arrayList;
    }

    /**
     * 查询用户拥有的权限
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public List<SysModule> queryModuleByRole(String roleId){
        List<SysModule> sysModules = userMapper.queryModuleByRole(roleId);
        return sysModules;
    }

    /**
     * 删除权限
     * @param code
     * @throws Exception
     */
    @Override
    public void deleteFuncByCode(String[] code){
        userMapper.delModule(code);
    }

    /**
     * 添加权限
     * @param moduleId
     * @param roleId
     * @throws Exception
     */
    @Override
    public void addFunc(String moduleId, String roleId){
        userMapper.addModule(moduleId,roleId);
    }

    /**
     * 树结构
     * @return
     * @throws Exception
     */
    @Override
    public List<ParentNodeVO> queryParentNode(String roleId){
        List<ParentNodeVO> parentNodeVOS = userMapper.queryParentNode(roleId);
        return parentNodeVOS;
    }

    @Override
    public List<String> queryFuncByUser(String username) throws Exception {
        List<String> strings = userMapper.queryModuleByName(username);
        return strings;
    }

}
